<template >
    <div>

        <div>
            <el-row :gutter="15">
                <el-form ref="elForm" :model="form" :rules="rules" size="medium" label-width="100px">
                    <el-col :span="12">
                        <el-form-item label="患者id" prop="field102">
                            <el-input placeholder="请输入内容" v-model="form.id" class="input-with-select">
                                <el-button slot="append" icon="el-icon-search" @click="queryPatient"></el-button>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="患者姓名" prop="field101">
                            <el-input v-model="form.pName" placeholder="请输入患者姓名" clearable :style="{ width: '100%' }">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="患者卡号" prop="field102">
                            <el-input v-model="form.pNo" placeholder="请输入患者卡号" clearable :style="{ width: '100%' }">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="患者年龄" prop="field105">
                            <el-select v-model="form.pAge" placeholder="请选择患者年龄" clearable :style="{ width: '100%' }">
                                <el-option v-for="(item, index) in field105Options" :key="index" :label="item.label"
                                    :value="item.value" :disabled="item.disabled"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="出生日期" prop="field106">
                            <el-date-picker v-model="form.pStartDate" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                                :style="{ width: '100%' }" placeholder="请选择出生日期" clearable></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="性别" prop="field104">
                            <el-select v-model="form.pSex" placeholder="请选择性别" clearable :style="{ width: '100%' }">
                                <el-option v-for="(item, index) in field104Options" :key="index" :label="item.label"
                                    :value="item.value" :disabled="item.disabled"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="手机号码" prop="field103">
                            <el-input v-model="form.pPhone" placeholder="请输入手机号码" clearable :style="{ width: '100%' }">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="证件号码" prop="mobile">
                            <el-input v-model="form.pCard" placeholder="请输入证件号码" :maxlength="11" show-word-limit
                                clearable prefix-icon='el-icon-mobile' :style="{ width: '100%' }"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="9">
                        <el-form-item label="地址" prop="field110">
                            <el-input v-model="form.pProvince" placeholder="请输入地址" clearable :style="{ width: '100%' }">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="15">
                        <el-form-item label="" prop="field112">
                            <el-input v-model="form.pPlace" placeholder="请输入详细地址" clearable :style="{ width: '100%' }">
                            </el-input>
                        </el-form-item>
                    </el-col>

                </el-form>
            </el-row>
        </div>
        <!-- 处方列表 -->
        <el-row :gutter="20">
            <el-col :span="12">
                <div style="float:right; margin-top: 10px;margin-bottom: 10px;">
                    <el-form :inline="true">
                        <template slot-scope="scope">
                            <el-col :span="1.5">
                                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                                    @click="handleDelete" v-hasPermi="['drugs:prescript:remove']">批量删除</el-button>
                            </el-col>
                        </template>
                    </el-form>
                </div>
                <div class="grid-content bg-purple">
                    <el-table v-loading="loading" :data="prescriptList" @selection-change="handleSelectionChange">
                        <el-table-column type="selection" width="55" align="center" />
                        <el-table-column label="序号" align="center" prop="dCode" width="auto" />
                        <el-table-column label="名称" align="center" prop="dName" width="auto" />
                        <el-table-column label="单次用量" align="center" prop="dCount" width="auto" />
                        <el-table-column label="用法" align="center" prop="dUsage" width="auto" />
                        <el-table-column label="频度" align="center" prop="dPindu" width="50px" />
                        <el-table-column label="天数" align="center" prop="dDay" width="50px" />
                        <el-table-column label="数量" align="center" prop="pNum" width="50px" />
                        <el-table-column label="单价" align="center" prop="dPrice" width="auto" />
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template slot-scope="scope">
                                <el-button size="mini" type="text" icon="el-icon-delete"
                                    @click="handleDelete(scope.row)"
                                    v-hasPermi="['drugs:prescript:remove']">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div style="margin-left: 250px">
                        <span>次方合计{{ sNum }}个处方</span>
                        <span>&nbsp 共计{{ sPrise }}元</span>
                    </div>

                </div>



            </el-col>
            <!-- 药品列表 -->
            <el-col :span="11">

                <div class="grid-content bg-purple">
                    <div style=" margin-top: 10px;">
                        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                            label-width="68px">
                            <el-form-item label="药品名称" prop="dName">
                                <el-input v-model="queryParams.dName" placeholder="请输入药品名称" clearable
                                    @keyup.enter.native="handleQuery" />
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" icon="el-icon-search" size="mini"
                                    @click="handleQuery">搜索</el-button>
                                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                    <el-table v-loading="loading" :data="drugsList">

                        <el-table-column label="药品名称" align="center" prop="dName" />

                        <el-table-column label="药品规格" align="center" prop="dSpec" />

                        <el-table-column label="药品库存" align="center" prop="dStock" />
                        <el-table-column label="药品价格" align="center" prop="dPrice" />
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template slot-scope="scope">
                                <el-col :span="1.5">
                                    <el-button type="primary" plain icon="el-icon-plus" size="mini"
                                        @click="handleAdd(scope.row)"
                                        v-hasPermi="['drugs:prescript:add']">添加</el-button>
                                </el-col>
                                <!-- <el-button size="mini" type="text" icon="el-icon-edit">添加</el-button> -->
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <!-- 导航栏部分 -->
                <el-row>
                    <el-col :span="24">
                        <div class="grid-content bg-purple-dark" style="float:right; margin-top: 10px;">
                            <el-button type="primary" @click="save">保存</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-col>
        </el-row>
        <!-- 添加或修改prescript对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">

                <el-form-item label="药品id" prop="pDrugsId">
                    <el-input v-model="form.pDrugsId" placeholder="请输入关联药品id" />
                </el-form-item>
                <el-form-item label="数量" prop="pNum">
                    <el-input v-model="form.pNum" placeholder="请输入数量" />
                </el-form-item>
                <el-form-item label="病历id" prop="pCasesId">
                    <el-input v-model="form.pCasesId" placeholder="请输入关联病历id" />
                </el-form-item>
                <el-form-item label="医生id" prop="pStaffId">
                    <el-input v-model="form.pStaffId" placeholder="请输入关联医生id" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

    </div>

</template>


<script>
import { listDrugs, getDrugs, delDrugs, updateDrugs } from "@/api/drugs/drugs";
import { listPrescript, getPrescript, delPrescript, addPrescript, updatePrescript } from "@/api/drugs/prescript";
import { listPatient, getPatient, delPatient, addPatient, updatePatient } from "@/api/patientsystem/patient";

export default {
    data() {
        return {
            // 显示搜索条件
            showSearch: true,
            drugsList: [],
            // prescript表格数据
            prescriptList: [],
            // 选中数组
            ids: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            sNum: '',
            sPrise: 0,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                dName: null,
                dType: null,
            },
            // 表单参数
            // form: {},
            // 表单校验
            rules: {
            },
            //显示面板
            showTemplate: true,
            form: {
                id: '',
                pName: null,
                pNo: null,
                pAge: null,
                pStartDate: null,
                pSex: null,
                pPhone: null,
                pCard: null,
                pProvince: null,
                pPlace: null,
                pArea: null,
            },
        }
    },
    methods: {
        save(){
            this.$message("保存成功")
        },
        queryPatient() {
            getPatient(this.form.id).then(res => {
                this.form = res.data
            });
            listPrescript(this.queryParams).then(response => {
                var price = 0
                this.prescriptList = response.rows;
                this.total = response.total;
                this.loading = false;
                this.sNum = response.total;
                this.prescriptList.forEach(function (e) {
                    price = price + (e.pNum * e.dPrice);
                    return price;
                });
                this.sPrise = price;
                console.log(this.sNum)
            });
        },

        // 药品模糊查询
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.open = true;
            this.title = "添加药品";
            this.form.pDrugsId = row.id
            console.log(row)

        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                pDrugsId: null,
                pNum: null,
                pCasesId: null,
                pStaffId: null
            };
            this.resetForm("form");
        },
        /** 提交按钮 */
        submitForm() {
            addPrescript(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();

            });
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除prescript编号为"' + ids + '"的数据项？').then(function () {
                return delPrescript(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },

        /** 查询drugs列表 */
        getList() {
            listDrugs(this.queryParams).then(response => {
                this.drugsList = response.rows;
                this.total = response.total;

            });

        },
    },








    created() {
        this.getList();


    }
}
</script>

<style>

</style>
